package fun.tan90.easy.binlog.manager.rest;

import fun.tan90.easy.binlog.manager.model.cmd.BinlogOutputRuleAddCmd;
import fun.tan90.easy.binlog.manager.model.cmd.BinlogOutputRuleUpdateCmd;
import fun.tan90.easy.binlog.manager.model.vo.BinlogOutputRuleVo;
import fun.tan90.easy.binlog.manager.service.BinlogOutputRuleService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 控制层。
 *
 * @author 陈浩杰
 * @since 2023-09-12
 */
@RestController
@RequestMapping("/binlog-output-rule")
public class BinlogOutputRuleController {

    @Resource
    private BinlogOutputRuleService binlogOutputRuleService;

    /**
     * 命中规则测试
     *
     * @param masterId
     * @param schemaRegular
     * @param tableRegular
     * @return
     */
    @GetMapping("regular/test")
    public Map<String, List<String>> regularTest(String masterId, String schemaRegular, String tableRegular) {
        return binlogOutputRuleService.regularTest(masterId, schemaRegular, tableRegular);
    }

    /**
     * 主节点输出方式规则列表
     *
     * @param masterId
     * @return
     */
    @GetMapping("list")
    public List<BinlogOutputRuleVo> list(String masterId) {
        return binlogOutputRuleService.list(masterId);
    }

    /**
     * 新增输出规则
     *
     * @param addCmd
     */
    @PostMapping
    public void addBinlogOutputRule(@RequestBody @Validated BinlogOutputRuleAddCmd addCmd) {
        binlogOutputRuleService.addBinlogOutputRule(addCmd);
    }

    /**
     * 编辑输出规则
     *
     * @param updateCmd
     */
    @PutMapping
    public void updateBinlogOutputRule(@RequestBody @Validated BinlogOutputRuleUpdateCmd updateCmd) {
        binlogOutputRuleService.updateBinlogOutputRule(updateCmd);
    }

    /**
     * 删除输出规则
     *
     * @param id
     */
    @DeleteMapping("{id}")
    public void delBinlogOutputRule(@PathVariable("id") String id) {
        binlogOutputRuleService.delBinlogOutputRule(id);
    }

    /**
     * 切换输出规则状态
     *
     * @param id
     */
    @PutMapping("/switch-status/{id}")
    public void switchBinlogOutputRuleStatus(@PathVariable("id") String id) {
        binlogOutputRuleService.switchBinlogOutputRuleStatus(id);
    }
}